<?php
        include "config.php";
        date_default_timezone_set("Asia/Shanghai");
        if($_POST){
            $sql = "select * from diary where title like'%".$_POST['title']."%'";
            $sql_count = "select count(*) from diary where title like'%".$_POST['title']."%'";
            $text = "匹配记录数：";
        }else{
            $sql = 'select * from diary order by addtime DESC';
            $sql_count = 'select count(*) from diary';
            $text = "留言总数: ";
        }
        try{
            $dbh = dbConnect();
            $sth = $dbh->query($sql);
            $rows = $sth->fetchAll();
            $result = $dbh->query($sql_count);
        }catch(PDOException $e){
            print('Error:'.$e->getMessage());
            die();
        }  
        $dbh = null;
    ?>
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index</title>
    <link rel="stylesheet" href="./css/index.css">
    <link rel="stylesheet" href="./css/common.css">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery-cookie/1.0/jquery.cookie.min.js"></script>
</head>
<body class="index-body">
        <div class="index-title-header">
            <h1>论坛主页<br><?= "你好！".$_COOKIE["username"]?></h1>   
        </div>

        <div class="index-title-menu">
            <form action="index.php" method="POST">
                <input type="text" name="title" class="index-form-input-text" placeholder="输入标题查询">
                <input type="submit" class="index-form-input-submit" value="查询">
            </form>
            <input type="button" class="index-button" value="写留言" onclick="window.location.href='write.php'">
            <input type="button" class="index-button" value="返回主页" onclick="window.location.href='index.php'">
            <input type="button" class="index-button index-out" value="退出登录" onclick="window.location.href='login.php'">
            <div class="index-count">
                <?= $text.$result->fetchColumn()?>
            </div>
        </div>
        <?php foreach($rows as $row):?>
            <div class="index-text-wrap">
            <div class="index-article-item">
                <div class="index-article-img">
                    <img src="./img/background.jpg" alt="">
                </div>
                <div class="index-article-wrap">
                    <div class="index-article-title index-text-item">
                        <!-- <h1>TITLE</h1> -->
                        <?= "<h1>{$row['title']}</h1>"?>
                    </div>
                    <div class="index-article-content index-text-item">
                        <!-- <span>CONTENT</span> -->
                        <?= "<span style='font-weight:900;'>メッセージの内容：</span><span>{$row['content']}</span>"?>
                    </div>
                    <div class="index-article-date index-text-item">
                        <!-- <span>DATE</span> -->
                        <?= "<span>伝言時間：{$row['addtime']}</span>"?>
                        <form action="edit.php">
                            <input type="text" name="id" value="<?= $row['id']?>" hidden>
                            <input type="submit" value="修改">
                        </form>
                        <form action="delete.php">
                            <input type="text" name="id" value="<?= $row['id']?>" hidden>
                            <input type="submit" value="删除">
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <?php endforeach;?>
</body>
</html>